﻿using STHPMS1.BLL.B_Core;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ServiceStack.OrmLite;
using STHPMS1.ICore.I_DAL;
using STHPMS1.Common.C_Entity.Stmpms1;
using System.Linq;
using STHPMS1.Common.C_Enum;

namespace STHPMS1.BLL.B_Bus
{
    public class B_RoadwayBus : BllBase, IB_RoadwayBill
    {
        public void Save<T>(T entity)
            where T : class ,IB_Roadway
        {
            if (entity.Rdwayid == null) throw new Exception("路线编号不能为空!");
            if (entity.Name == null) throw new Exception("路线名称不能为空!");
            if (entity.Admgrd == 0) throw new Exception("行政等级不能为空!");
            if (entity.Endmp <= entity.Beginmp) throw new Exception("终点桩号要大于起点桩号!");

            entity.Importdate = DateTime.Now;
            this.Scene.Dal.Sthpms1.Save(entity);
        }

        public void Remove(IB_Roadway entity)
        {
            this.Scene.Dal.Sthpms1.DeleteById<B_Roadway>(entity.Rdwayid);
        }

        public double GetLength(string rdwayid)
        {
            return this.Scene.Dal.Sthpms1.B_Roadway.GetLength(rdwayid);
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IB_Roadway
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info,k=>k.Importdate,true);
        }

        public double GetNewHistoryVersion(string rdwayid,bool increase = true)
        {
            double ver = this.Scene.Dal.Sthpms1.Scalar<B_Roadway, double>(p => p.His_Version ?? 0,k=>k.Rdwayid==rdwayid);
            if (increase) ver++;
            return ver;
        }

        public Dictionary<string, string> Dicts()
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            return dao.Dictionary<string, string>(dao.From<B_Roadway>().Select(x => new { x.Rdwayid, x.Name }));
        }

        public  bool CheckRoadDir(string rdwayid, int dir)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            return dao.B_Roadway.CheckRoadDir(rdwayid, dir);
        }

        public string GetRoadnetName(string rdwayid)
        {
            //ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            //List<string> list = dao.Column<string>(dao.From<B_Roadway>().Select(x => x.RoadnetName).Where(p => p.Rdwayid == rdwayid));
            //if (list == null)
            //{
            //    return null;
            //}
            //else if (list.Count == 0)
            //{
            //    return null;
            //}
            //else
            //{
            //    return list[0];
            //}
            return "";
        }


        /// <summary>
        /// 判断该道路编号是否存在
        /// </summary>
        /// <param name="rdId"></param>
        /// <returns></returns>
        public bool Exists(string rdId)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            return dao.Exists<B_Roadway>(q => q.Rdwayid == rdId );
        }

        /// <summary>
        /// 根据编号查询路线对象
        /// </summary>
        /// <param name="rdid"></param>
        /// <returns></returns>
        public List<T> SearchById<T>(string rdid) where T : class,IB_Roadway
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            return dao.Select<T>(q => q.Rdwayid == rdid);
        }

        /// <summary>
        /// 判断起终桩号是否在指定线路中
        /// </summary>
        /// <param name="rdId"></param>
        /// <param name="bmp"></param>
        /// <param name="emp"></param>
        /// <returns></returns>
        public bool ExistsMP(string rdId, double bmp, double emp)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            return dao.Exists<B_Roadway>(q => q.Rdwayid == rdId && q.Beginmp <= bmp && q.Endmp >= emp);
        }

        /// <summary>
        /// 递归行政区域
        /// </summary>
        /// <param name="dict"></param>
        /// <param name="codes"></param>
        private void RecursiveCode(Dictionary<int, string> dict, IEnumerable<IRegionCodeInfo> codes)
        {
            if (codes == null) return;
            foreach (var item in codes)
            {
                if (item.id != 0)
                    dict.Add(item.id, item.text);
                RecursiveCode(dict, item.child);
            }
        }
        /// <summary>
        /// 返回行政区域的集合
        /// </summary>
        /// <returns></returns>
        public Dictionary<int, string> GetADMDIV()
        {
            if (Common.C_Definition.MyCacheTools.IsCacheExist("qy"))
            {
                 return (Dictionary<int, string>)Common.C_Definition.MyCacheTools.GetCache("qy");
            }
            else
            {
                Dictionary<int, string> temp = new Dictionary<int, string>();
                RecursiveCode(temp, this.Scene.Data.RegionCodeEnum);
                 Common.C_Definition.MyCacheTools.SetCache("qy", temp, 24);
                return temp;
            }
        }

        public IEnumerable<T> GetAll<T>() where T : class, IB_Roadway
        {
            return this.Scene.Dal.Sthpms1.Select<T>();
        }

        public int GetAdmdiv(string rdwayid)
        {
            //var t = this.Scene.Dal.Sthpms1.Single<B_Roadway>(q => q.Rdwayid == rdwayid);
            return this.Scene.Dal.Sthpms1.B_Roadway.GetAdmdiv(rdwayid);
            //return Convert.ToInt32(this.Scene.Dal.Sthpms1.Single<IB_Roadway>(q=>q.Rdwayid==rdwayid).Admdiv);
            //return this.Get<IB_Roadway>()
        }


        public IEnumerable<T> SearchRoad<T>()
            where T : class, IB_Roadway
        {
            return this.Scene.Dal.Sthpms1.B_Roadway.SearchRoad<T>();
        }


        /// <summary>
        /// 得到路线的技术等级
        /// </summary>
        /// <param name="rdwayid"></param>
        /// <returns></returns>
        //public int GetRoadRdClass(string rdwayid)
        //{
           // ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
           // List<int> list = dao.Column<int>(dao.From<B_Roadway>().Select(x => x.Rdclass_Dictid).Where(p => p.Rdwayid == rdwayid));
           //if (list.Count == 0)
           // {
           //     return 0;
           // }
           // else
           // {
           //     return list[0];
           // }
            //return 0;
        //}

        /// <summary>
        /// 全线闭合检查
        /// </summary>
        /// <param name="rdid"></param>
        /// <param name="dic"></param>
        public void CheckFull(string rdid,SortedDictionary<double,double> dic )
        {
            B_Roadway road = this.Scene.Dal.Sthpms1.B_Roadway.GetRoadObj(rdid) as B_Roadway;
            if(road == null)
            {
                throw new Exception("系统未能找到此路线");
            }

        }

        public int GetDir(string rdid)
        {
            return this.Scene.Dal.Sthpms1.B_Roadway.GetDir(rdid);
        }
    }
}
